package com.ruoyi.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.business.domain.DocDirectory;
import com.ruoyi.business.domain.FileExportType;
import com.ruoyi.business.domain.bo.FileExportTypeBo;
import com.ruoyi.business.domain.vo.DocDirectoryVo;
import com.ruoyi.business.domain.vo.FileExportTypeVo;
import com.ruoyi.business.mapper.DocDirectoryMapper;
import com.ruoyi.business.mapper.FileExportTypeMapper;
import com.ruoyi.business.service.IDocDirectoryService;
import com.ruoyi.business.service.IFileExportTypeService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringExtUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * ExportTypeService业务层处理
 *
 * @author ruoyi
 * @date 2025-01-15
 */
@RequiredArgsConstructor
@Service
public class FileExportTypeServiceImpl implements IFileExportTypeService {

    private final FileExportTypeMapper baseMapper;
    @Autowired
    private IDocDirectoryService docDirectoryService;
    @Autowired
    private DocDirectoryMapper docDirectoryMapper;

    /**
     * 查询ExportType
     */
    @Override
    public FileExportTypeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询ExportType列表
     */
    @Override
    public TableDataInfo<FileExportTypeVo> queryPageList(FileExportTypeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<FileExportType> lqw = buildQueryWrapper(bo);
        Page<FileExportTypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询ExportType列表
     */
    @Override
    public List<FileExportTypeVo> queryList(FileExportTypeBo bo) {
        LambdaQueryWrapper<FileExportType> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<FileExportType> buildQueryWrapper(FileExportTypeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<FileExportType> lqw = Wrappers.lambdaQuery();
        lqw.like(StringExtUtils.isNotBlank(bo.getName()), FileExportType::getName, bo.getName());
        return lqw;
    }

    /**
     * 新增ExportType
     */
    @Override
    public Boolean insertByBo(FileExportTypeBo bo) {
        FileExportType add = BeanUtil.toBean(bo, FileExportType.class);
        validEntityBeforeSave(add);
        String[] split = bo.getTypeId().split(",");
        LambdaQueryWrapper<DocDirectory> docWrapper = new LambdaQueryWrapper<DocDirectory>().in(DocDirectory::getId, split);
        List<DocDirectoryVo> docDirectoryVos = docDirectoryMapper.selectVoList(docWrapper);
        StringBuilder typeName = new StringBuilder();
        if (!docDirectoryVos.isEmpty()) {
            for (DocDirectoryVo docDirectoryVo : docDirectoryVos) {
                typeName.append(docDirectoryVo.getName()).append(";");
            }
        }
        add.setTypeName(typeName.toString());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改ExportType
     */
    @Override
    public Boolean updateByBo(FileExportTypeBo bo) {
        FileExportType update = BeanUtil.toBean(bo, FileExportType.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(FileExportType entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除ExportType
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
